<?php

$this->LoadHelper('MemberHelper');
$MemberHelper = new MemberHelper();

$this->LoadHelper('OrderHelper');
$OrderHelper = new OrderHelper();

$this->LoadHelper('StretchHelper');
$StretchHelper = new StretchHelper();

$member_info = $MemberHelper->getMember($this->UserConfig['Id']);
$this->Assign('member_info', $member_info);

if (empty($this->Param['option'])) {
    
    //通融
    $ajax = $this->Param['ajax'];
    $page = isset($this->Param['page']) ? $this->Param['page'] : 1;
    $rp = isset($this->Param['rp']) ? $this->Param['rp'] : PAGE_SIZE;
    $sr_msg = $this->LoadModel('Public')->getList('stretch_rules', '*', array('`sr_result` = ?' => '申请中'));
    for($i=0;$i<count($sr_msg);$i++){
        $sr_oid[$i]= $sr_msg[$i]['sr_oid'];
    }
    if ($ajax) {
        //$where = array(
        //    '`o_id` IN(?)' => $sr_oid
        //);
        if(!empty($this->Param['o_keyword'])){
            $where['`o_vip_name` = ?'] = $this->Param['o_keyword'];
        }else{
            $where['`o_id` IN(?)'] = $sr_oid;
        }
        $data = $OrderHelper->orderList($where, $page, $rp, $this->Param, $member_info);
        $json_data = array('total' => $data['count'], 'rows' => array());
        if (!empty($data['all']))
            foreach ($data['all'] AS $rowNum => $row) {
                $row['o_quit_time'] = empty($row['o_quit_time']) ? '' : date('Y-m-d H:i:s', $row['o_quit_time']);//退回时间
                $row['sign_check'] = $StretchHelper->signJudge($row['o_id']);
                $json_data['rows'][] = $row;
            }
        echo json_encode($json_data);
    } else {        
        echo $this->GetView('stretch_index.php');
    }
} else {
    switch($this->Param['option']){
        case 'list':
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if(!$order_check['success'])                ErrorMsg::Debug ('无权操作');
            $page = isset($this->Param['page']) ? $this->Param['page'] : 1;
            $rp = isset($this->Param['rp']) ? $this->Param['rp'] : PAGE_SIZE;
            $where = array(
                '`sr_oid` = ?' => $oid,
                '`sr_result` != ?' => 'del'
            );
            $data = $StretchHelper->signList($where, $page, $rp, $this->Param);
            $json_data = array('total' => $data['count'], 'list' => array(), 'page' => $page, 'pagetotal' => $data['pagetotal']);
            if (!empty($data['all'])){
                foreach ($data['all'] AS $rowNum => $row) {
                    $row['sr_time'] = date('Y-m-d', $row['sr_time']);
                    $json_data['list'][] = $row;
                }
            }
            ErrorMsg::Debug($json_data, true);
            break;
        case 'sign':
            $oid = empty($this->Param['oid']) ? ErrorMsg::Debug('参数错误') : $this->Param['oid'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($oid, $this->UserConfig['Id']);
            if(!$order_check['success'])                ErrorMsg::Debug ('无权操作');
            $this->Assign('oid',$oid);
            echo $this->GetView('stretch_sign.php'); 
            break;
        case 'signsave'://保存
            $data['sr_oid'] = empty($this->Param['oid']) ? ErrorMsg::Debug('订单ID值错误') : $this->Param['oid'];
            $data['sr_cause'] = empty($this->Param['sr_cause']) ? ErrorMsg::Debug('请选择通融原因') : $this->Param['sr_cause'];
            $data['sr_remark'] = $this->Param['sr_remark'];
            //检测订单是否为权限允许内操作
            $order_check = $OrderHelper->checkOrderper($data['oid'], $this->Param['uid']);
            if(!$order_check['success'])                ErrorMsg::Debug ('无权操作');
            $sign_judge = $StretchHelper->signJudge($this->Param['oid']);
            if(!$sign_judge)    ErrorMsg::Debug ('当前状态已不允许再次申请通融');
            
            $data['sr_uname'] = $member_info['u_realname'];
            $data['sr_uid'] = $this->UserConfig['Id'];
            $data['sr_time'] = NOW_TIME;
            $result = $StretchHelper->signSave($data);
            if($result['success'] == true){
                $MemberHelper->saveLog($this->UserConfig, '通融申请', json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('保存成功', true);
            }else{                
                ErrorMsg::Debug($do['msg']);
            }
            break;
        case 'remove'://删除
            $sr_id = empty($this->Param['sr_id']) ? ErrorMsg::Debug('ID值错误') : $this->Param['sr_id'];
            $sr_row = $StretchHelper->signRow($sr_id);
            if($sr_row['sr_result'] != '申请中'){
                ErrorMsg::Debug('当前订单不允许处理');
            }
            $result = $StretchHelper->signSave(array('sr_result' => 'del'), $sr_id, false);
            if($result['success'] == true){
                $MemberHelper->saveLog($this->UserConfig, '删除通融', json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('删除成功', true);
            }else{                
                ErrorMsg::Debug($result['msg']);
            }
            break;
        case 'audit':
            $sr_id = empty($this->Param['sr_id']) ? ErrorMsg::Debug('ID值错误') : $this->Param['sr_id'];
            $sr_row = $StretchHelper->signRow($sr_id);
            if($sr_row['sr_result'] != '申请中'){
                ErrorMsg::Debug('当前订单不允许处理');
            }
            $this->Assign('sr_id', $sr_id);
            echo $this->GetView('stretch_audit.php'); 
            break;            
        case 'auditsave':
            $sr_id = empty($this->Param['sr_id']) ? ErrorMsg::Debug('ID值错误') : $this->Param['sr_id'];
            $data['sr_result'] = empty($this->Param['sr_result']) ? ErrorMsg::Debug('处理结果不能为空') : $this->Param['sr_result'];
            $data['sr_deal_uid'] = $this->UserConfig['Id'];
            $data['sr_deal_uname'] = $member_info['u_realname'];
            $data['sr_deal_time'] = NOW_TIME;
            $result = $StretchHelper->signDetail($sr_id, $data);
            if($result['success'] == true){
                $MemberHelper->saveLog($this->UserConfig, '通融审核', json_encode($this->Param), $this->Controller.'_'.$this->Action);
                ErrorMsg::Debug('保存成功', true);
            }else{                
                ErrorMsg::Debug($result['msg']);
            }
            break;
        default :
    }
}